<?php

include_once("SweetBaseGenerator.php");

class SweetXMLGenerator extends SweetBaseGenerator
{
	public function generate()
	{		
		$xml = new SimpleXMLElement("<SweetTestSuite></SweetTestSuite>");

		$xml->addAttribute("tests",$this->sweetTestSuite->getNumberOfTests());
		$xml->addAttribute("passed",$this->sweetTestSuite->getNumberOfPassedTests());
		$xml->addAttribute("failed",$this->sweetTestSuite->getNumberOfFailedTests());
		$xml->addAttribute("errored",$this->sweetTestSuite->getNumberOfErroredTests());
		
		$tests = $this->sweetTestSuite->getTests();

		$i = 0;
		foreach($tests as $className => $reflectedMethods)
		{
			$xml->addChild("class");
			$xml->class[$i]->addAttribute("name",$className);
			
			$passed = $this->sweetTestSuite->getPassedTests();
			$xml->class[$i]->addChild("passed");
			$j = 0;
			if(is_array($passed) && sizeof($passed) > 0)
				foreach($passed[$className] as $methodName => $value)
				{
					$xml->class[$i]->passed[0]->addChild("method");
					$xml->class[$i]->passed[0]->method[$j]->addAttribute("name",$methodName);
					$j++;
				}
			
			$failed = $this->sweetTestSuite->getFailedTests();
			$xml->class[$i]->addChild("failed");
			$j = 0;
			
			if(is_array($failed) && sizeof($failed) > 0)
				foreach($failed[$className] as $methodName => $exception)
				{
					$xml->class[$i]->failed[0]->addChild("method");
					$xml->class[$i]->failed[0]->method[$j]->addAttribute("name",$methodName);
					$xml->class[$i]->failed[0]->method[$j]->addChild("exception");
					$xml->class[$i]->failed[0]->method[$j]->exception[0]->addAttribute("File",$exception->seFile);
					$xml->class[$i]->failed[0]->method[$j]->exception[0]->addAttribute("Line",$exception->seLine);
					$xml->class[$i]->failed[0]->method[$j]->exception[0]->addAttribute("Function",$exception->seFunction);
					$xml->class[$i]->failed[0]->method[$j]->exception[0]->addAttribute("Message",$exception->seMessage);
					$xml->class[$i]->failed[0]->method[$j]->exception[0]->addAttribute("Code",$exception->seCode);
					$j++;
				}			
				
			$errored = $this->sweetTestSuite->getErroredTests();
			$xml->class[$i]->addChild("errored");
			$j = 0;
			if(is_array($errored) && sizeof($errored) > 0)
				foreach($errored[$className] as $methodName => $exception)
				{
					$xml->class[$i]->errored[0]->addChild("method");
					$xml->class[$i]->errored[0]->method[$j]->addAttribute("name",$methodName);
					$xml->class[$i]->errored[0]->method[$j]->addChild("exception");
					$xml->class[$i]->errored[0]->method[$j]->exception[0]->addAttribute("Message",$exception->getMessage());
					$xml->class[$i]->errored[0]->method[$j]->exception[0]->addAttribute("File",$exception->getFile());
					$xml->class[$i]->errored[0]->method[$j]->exception[0]->addAttribute("Line",$exception->getLine());		
					$j++;
				}						
			$i++;
		}

		return $xml->asXML();
	}
}


?>